﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HiCore.WebApi.Example.DbContexts.DbEntity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace HiCore.WebApi.Example.DbContexts.Configurations
{
    public static class AuditableEntityConfiguration
    {
        public static EntityTypeBuilder<T> ConfigureAuditableEntityAggregateRoot<T>(this EntityTypeBuilder<T> builder) where T : AuditableEntityAggregateRoot
        {
            builder.Property(e => e.TenantId)
                .HasColumnName("TenantId")
                .HasColumnType("bigint");

            builder.Property(e => e.CreatedUserId)
                .HasColumnName("CreatedUserId")
                .HasColumnType("bigint");

            builder.Property(e => e.CreatedTime)
                .HasColumnName("CreatedTime")
                .HasColumnType("datetimeoffset(7)");

            builder.Property(e => e.LastUpdatedUserId)
                .HasColumnName("LastUpdatedUserId")
                .HasColumnType("bigint");

            builder.Property(e => e.LastUpdatedTime)
                .HasColumnName("LastUpdatedTime")
                .HasColumnType("datetimeoffset(7)");

            builder.Property(e => e.IsDeleted)
                .HasColumnName("IsDeleted")
                .HasColumnType("tinyint");

            builder.Property(e => e.DeleteUserId)
                .HasColumnName("DeleteUserId")
                .HasColumnType("bigint");

            builder.Property(e => e.DeleteTime)
                .HasColumnName("DeleteTime")
                .HasColumnType("datetimeoffset(7)");
            return builder;
        }

        public static EntityTypeBuilder<T> ConfigureAuditableEntityAggregateRoot<T, TKey>(this EntityTypeBuilder<T> builder) where T : AuditableEntityAggregateRoot<TKey>
        {
            builder.Property(e => e.TenantId)
                .HasColumnName("TenantId")
                .HasColumnType("bigint");

            builder.Property(e => e.CreatedUserId)
                .HasColumnName("CreatedUserId")
                .HasColumnType("bigint");

            builder.Property(e => e.CreatedTime)
                .HasColumnName("CreatedTime")
                .HasColumnType("datetimeoffset(7)");

            builder.Property(e => e.LastUpdatedUserId)
                .HasColumnName("LastUpdatedUserId")
                .HasColumnType("bigint");

            builder.Property(e => e.LastUpdatedTime)
                .HasColumnName("LastUpdatedTime")
                .HasColumnType("datetimeoffset(7)");

            builder.Property(e => e.IsDeleted)
                .HasColumnName("IsDeleted")
                .HasColumnType("tinyint");

            builder.Property(e => e.DeleteUserId)
                .HasColumnName("DeleteUserId")
                .HasColumnType("bigint");

            builder.Property(e => e.DeleteTime)
                .HasColumnName("DeleteTime")
                .HasColumnType("datetimeoffset(7)");
            return builder;
        }

        public static EntityTypeBuilder<T> ConfigureAuditableEntity<T>(this EntityTypeBuilder<T> builder) where T : AuditableEntity
        {
            builder.Property(e => e.TenantId)
                .HasColumnName("TenantId")
                .HasColumnType("bigint");

            builder.Property(e => e.CreatedUserId)
                .HasColumnName("CreatedUserId")
                .HasColumnType("bigint");

            builder.Property(e => e.CreatedTime)
                .HasColumnName("CreatedTime")
                .HasColumnType("datetimeoffset(7)");

            builder.Property(e => e.LastUpdatedUserId)
                .HasColumnName("LastUpdatedUserId")
                .HasColumnType("bigint");

            builder.Property(e => e.LastUpdatedTime)
                .HasColumnName("LastUpdatedTime")
                .HasColumnType("datetimeoffset(7)");

            builder.Property(e => e.IsDeleted)
                .HasColumnName("IsDeleted")
                .HasColumnType("tinyint");

            builder.Property(e => e.DeleteUserId)
                .HasColumnName("DeleteUserId")
                .HasColumnType("bigint");

            builder.Property(e => e.DeleteTime)
                .HasColumnName("DeleteTime")
                .HasColumnType("datetimeoffset(7)");
            return builder;
        }

        public static EntityTypeBuilder<T> ConfigureAuditableEntity<T, TKey>(this EntityTypeBuilder<T> builder) where T : AuditableEntity<TKey>
        {
            builder.Property(e => e.TenantId)
                .HasColumnName("TenantId")
                .HasColumnType("bigint");

            builder.Property(e => e.CreatedUserId)
                .HasColumnName("CreatedUserId")
                .HasColumnType("bigint");

            builder.Property(e => e.CreatedTime)
                .HasColumnName("CreatedTime")
                .HasColumnType("datetimeoffset(7)");

            builder.Property(e => e.LastUpdatedUserId)
                .HasColumnName("LastUpdatedUserId")
                .HasColumnType("bigint");

            builder.Property(e => e.LastUpdatedTime)
                .HasColumnName("LastUpdatedTime")
                .HasColumnType("datetimeoffset(7)");

            builder.Property(e => e.IsDeleted)
                .HasColumnName("IsDeleted")
                .HasColumnType("tinyint");

            builder.Property(e => e.DeleteUserId)
                .HasColumnName("DeleteUserId")
                .HasColumnType("bigint");

            builder.Property(e => e.DeleteTime)
                .HasColumnName("DeleteTime")
                .HasColumnType("datetimeoffset(7)");
            return builder;
        }

    }
}
